package me.w70.bot.account;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import me.w70.bot.Bot;

public class AccountsGUI extends JDialog implements ActionListener,
		ListSelectionListener {

	private Dimension size = new Dimension(500, 500);

	JList accs;

	JTextField user = new JTextField(10);

	JPasswordField pass = new JPasswordField(10);

	DefaultListModel jl;

	public AccountsGUI(Window parent) {

		super(parent, "Edit Accounts", ModalityType.APPLICATION_MODAL);
		Bot.accountHandler.loadAccounts();
		jl = new DefaultListModel();
		for (String username : Bot.getAccounts().keySet()) {
			jl.addElement(username);
		}

		accs = new JList(jl);

		accs.addListSelectionListener(this);
		accs.setPreferredSize(new Dimension(100, 100));

		add(accs, BorderLayout.NORTH);
		JPanel i = new JPanel();

		i.setLayout(new BoxLayout(i, BoxLayout.Y_AXIS));
		i.setSize(new Dimension(10, 5));

		i.add(Box.createRigidArea(new Dimension(0, 25)));
		i.add(new JLabel("Username:"));
		i.add(user);
		i.add(new JLabel("Password:"));
		i.add(pass);
		i.add(Box.createRigidArea(new Dimension(0, 50)));

		add(i, BorderLayout.WEST);

		JPanel buts = new JPanel();

		buts.setLayout(new BoxLayout(buts, BoxLayout.X_AXIS));
		JButton add = new JButton("Add");
		add.addActionListener(this);
		JButton rem = new JButton("Remove");
		rem.addActionListener(this);
		JButton app = new JButton("Apply");
		app.addActionListener(this);
		JButton ok = new JButton("OK");
		ok.addActionListener(this);
		JButton can = new JButton("Cancel");
		can.addActionListener(this);
		buts.add(add);
		buts.add(rem);
		buts.add(Box.createRigidArea(new Dimension(90, 0)));
		buts.add(app);

		buts.add(ok);
		buts.add(can);

		add(buts, BorderLayout.SOUTH);
		// add(new JLabel("LOL"), BorderLayout.CENTER);
		setPreferredSize(new Dimension(size.width, size.height - 200));
		pack();
	}

	public void actionPerformed(ActionEvent e) {

		if (e.getActionCommand().equals("Apply")
				|| e.getActionCommand().equals("OK")) {
			Bot.accountHandler.addAccount(user.getText(), pass.getText());
			Bot.accountHandler.saveAccounts();
			if (e.getActionCommand().equals("OK")) {
				setVisible(false);
			}
			Bot.addMessage(this, "Accounts saved.");
		}

		if (e.getActionCommand().equals("Add")) {
			addAccountList();
		}

		if (e.getActionCommand().equals("Cancel")) {
			setVisible(false);
		}

		if (e.getActionCommand().equals("Remove")) {

			Bot.accountHandler.removeAccount(username);
			jl.remove(accs.getSelectedIndex());
			user.setText("");
			pass.setText("");
		}

	}

	private void addAccountList() {
		if (user.getText().equals("") || user.getText() == null
				) {
			jl.addElement("New");
		} else {
			jl.addElement(user.getText());
			Bot.accountHandler.addAccount(user.getText(), pass.getText());
		}
	}

	int index = -1;

	String username = "";

	String password = "";

	public void valueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting() == false) {

			if (accs.getSelectedIndex() == -1) {
				// No selection, disable fire button.

			} else {
				// Selection, enable the fire button.
				try {
					index = accs.getSelectedIndex();
					username = accs.getModel().getElementAt(
							accs.getSelectedIndex()).toString();
					password = Bot.getAccounts().get(username).getPassword();
					user.setText(username);
					pass.setText(password);
				} catch (Exception f) {

				}
			}
		}

	}

}
